package com.example.rjb_shoping.controller;

import com.example.rjb_shoping.config.MsgResponse;
import com.example.rjb_shoping.entity.RjbCommodity;
import com.example.rjb_shoping.entity.RjbCustomer;
import com.example.rjb_shoping.query.UserQuery;
import com.example.rjb_shoping.service.RjbCommodityService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * (RjbCommodity)表控制层
 *
 * @author makejava
 * @since 2021-06-09 20:42:45
 */
@RestController
@RequestMapping("rjbCommodity")
public class RjbCommodityController {
    /**
     * 服务对象
     */
    @Resource
    private RjbCommodityService rjbCommodityService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public RjbCommodity selectOne(Integer id) {
        return this.rjbCommodityService.queryById(id);
    }

    /**
     * 获取商品信息
     *
     * @param cid
     * @return
     */
    @GetMapping("fruitList")
    public MsgResponse fruit_list(Integer cid, HttpSession session) {
        RjbCustomer attribute = (RjbCustomer) session.getAttribute("user");
        // System.out.println(attribute);
        return MsgResponse.success().add("date", rjbCommodityService.fruit_list(cid));
    }

    @PostMapping("insert")
    public MsgResponse insert(@RequestBody RjbCommodity rjbCommodity) {
        rjbCommodity.setMCost(rjbCommodity.getMOriginal());
        return MsgResponse.success().add("data", this.rjbCommodityService.insert(rjbCommodity));
//        return null;
    }

    /**
     * 查询所有
     *
     * @param status
     * @return
     */
    @GetMapping("fruitStatus")
    public List<RjbCommodity> fruitStatus(Integer status) {
        return rjbCommodityService.fruitStatus(status);
    }

    /**
     * 删除商品
     *
     * @param id
     * @return
     */
    @GetMapping("deleteOne")
    public boolean deleteOne(Integer id) {
        return rjbCommodityService.deleteByIdOne(id);
    }

    /**
     * 修改商品信息
     *
     * @param rjbCommodity
     * @return
     */
    @PostMapping("updateOne")
    public RjbCommodity updateOne(@RequestBody RjbCommodity rjbCommodity) {
        // System.out.println(rjbCommodity);
        return rjbCommodityService.update(rjbCommodity);
    }

    @GetMapping("selectAllFruit")
    public List<RjbCommodity> selectAllFruit() {
        // System.out.println(rjbCommodity);
        return this.rjbCommodityService.selectAllFruit();
    }
    @GetMapping("selectAllFlower")
    public List<RjbCommodity> selectAllFlower() {
        // System.out.println(rjbCommodity);
        return this.rjbCommodityService.selectAllFlower();
    }

    /**
     * 根据商品名称进行模糊查询
     *
     * @param name
     * @param status
     * @return
     */
    @GetMapping("likeAll")
    public List<RjbCommodity> likeAll(String name, Integer status) {
        return rjbCommodityService.likeAll(name, status);
    }

    /**
     * 分页查询
     *
     * @param status
     * @param
     * @return
     */
    @GetMapping("limitList")
    public PageInfo<RjbCommodity> limitList(
            @RequestParam(name = "status", required = true) Integer status,
            @RequestParam(name = "pageNum", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", required = true) Integer pageSize,
            @RequestParam(name = "name", required = true, defaultValue = "") String name) {
        UserQuery userQuery = new UserQuery();
        userQuery.setName(name);
        userQuery.setPageNum(pageNum);
        userQuery.setPageSize(pageSize);
        return rjbCommodityService.limitList(status, userQuery);
    }

    @PostMapping("selectLimit")
    public List<RjbCommodity> selectLimit(Integer start,Integer limit){
        int page = (start-1)*limit;
      return this.rjbCommodityService.selectLimit(page, limit);
    }
    @GetMapping("selectFruitNum")
    public int selectFruitNum(){
        return this.rjbCommodityService.selectFruitNum();
    }
    @PostMapping("selectLimitFlower")
    public List<RjbCommodity> selectLimitFlower(Integer start,Integer limit){
        int page = (start-1)*limit;
        return this.rjbCommodityService.selectLimitFlower(page, limit);
    }
    @GetMapping("selectFlowerNum")
    public int selectFlowerNum(){
        return this.rjbCommodityService.selectFlowerNum();
    }
}
